//
//  PopoverAnimator.swift
//  WeiBo
//
//  Created by wukeng on 16/8/23.
//  Copyright © 2016年 wukeng. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    //MARK:- 对外提供的属性
    var presentedFrame : CGRect = CGRectZero
    var isPresented : Bool = false
    var callBack : ((presented : Bool) -> ())?
    
    //MARK:- 自定义构造函数
    init(callBack : ((Presented : Bool) -> ())) {
        self.callBack = callBack
    }
    
}

// MARK: - 自定义转场代理的方法
extension PopoverAnimator : UIViewControllerTransitioningDelegate{
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?{
        let presented = WKPresentationController(presentedViewController: presented, presentingViewController: presenting)
        presented.presentedFrame = presentedFrame
        return presented
    }
    //自定义弹出动画
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self;
    }
    //自定义消失动画
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self;
    }
}
// MARK: - 弹出和消失动画代理方法
extension PopoverAnimator : UIViewControllerAnimatedTransitioning{
    //动画执行的时间
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        return 0.5
    }
    //获取“转场的上下文”：可以通过转场上下文获取弹出的View和消失的View
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        isPresented ?  animationForDismissedView(transitionContext) : animationForPresentedView(transitionContext)
    }
    
    //自定义弹出动画
    private func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning){
        //获取弹出的View
        let presentView = transitionContext.viewForKey(UITransitionContextToViewKey)!
        
        //弹出的View添加到containerView中
        transitionContext.containerView()?.addSubview(presentView)
        //执行动画
        presentView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        presentView.layer.anchorPoint = CGPointMake(0.5, 0)
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            presentView.transform = CGAffineTransformIdentity
        }) { (_) in
            //必须告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
            self.isPresented = true
            self.callBack!(presented : self.isPresented)
        }
    }
    //自定义消失动画
    private func animationForDismissedView(transitionContext: UIViewControllerContextTransitioning){
        //获取消失的view
        let dismissView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        //执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
            dismissView.transform = CGAffineTransformMakeScale(1.0, 0.0001)
        }) { (_) in
            transitionContext.completeTransition(true)
            dismissView.removeFromSuperview()
            self.isPresented = false
            self.callBack!(presented : self.isPresented)
        }
        
        
    }
    
}
